package com.zx.chatclient.fx.controller;

import java.net.URL;
import java.util.ResourceBundle;

import com.zx.chatclient.dto.IMMessage;
import com.zx.chatclient.dto.IMUser;
import com.zx.chatclient.netty.Client;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

/**
 * @author xunzheng
 *
 */
public class ChatViewController implements ControlledStage, Initializable {

	private StageController myController;

	@FXML
	private TextArea msgText;

	@FXML
	private TextArea sendMsgText;

	@FXML
	private Button sendButton;
	
	@FXML
	private Label senderLabel;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		senderLabel.setVisible(false);
	}

	@Override
	public void setStageController(StageController stageController) {
		this.myController = stageController;
	}
	
	public void sendMsg(ActionEvent event) {
		IMUser sender = Client.getUser();
		IMUser reciever = new IMUser(senderLabel.getText().trim(), null, null);
		IMMessage message = new IMMessage("1", "1", sender, "1", reciever, sendMsgText.getText().trim(),null,null);
		try {
			Client.getClientHander().sendMsg(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(msgText.getText()==null) {
			msgText.setText(Client.getUser().getUname()+":"+sendMsgText.getText()+"\n");
		}else {
			msgText.setText(msgText.getText()+Client.getUser().getUname()+":"+sendMsgText.getText()+"\n");
		}
		sendMsgText.setText("");
	}
}
